@using viBook.Models
@model viBook.Models.Book
@{
    ViewBag.Title = "Details";
    var categories = ViewBag.CategoryCollection as IEnumerable<SelectListItem>;
    var imageUrl = string.IsNullOrEmpty(@Model.CoverUrl) ? @Url.Content("~/Images/DefaultbookImage.png") : @Model.CoverUrl;
}
<link href="../../Content/BookTemplate.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/jquery.autocomplete.min.js" type="text/javascript"></script>
<link href="../../Content/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
<link href="../../Scripts/uploadify/uploadify.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/uploadify/swfobject.js" type="text/javascript"></script>
<script src="../../Scripts/uploadify/jquery.uploadify.v2.1.4.min.js" type="text/javascript"></script>

<h2>Details</h2>
 @using (Html.BeginForm("Edit", "Books", FormMethod.Post, new { enctype = "multipart/form-data" }))
 { 
      @Html.ValidationSummary(true)
<fieldset>
    <legend>Book</legend>
    <div class="container">
        <div class="span-24" style="background-color:Gray">

        <input  type="submit"  value="Save Changes"/>
        </div>
        <div class="span-10">
            <img  src=@imageUrl width="300" height="400"/>            
            <input id="file_upload" name="file_upload" type="file" />             
            <input type="hidden" id="filesUploaded" name="filesUploaded" />
           </div>
        <div class="span-10">
           <div class="editor-label">
        @Html.LabelFor(model => model.Title)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Title)
        @Html.ValidationMessageFor(model => model.Title)
    </div>
    <div  class="editor-label">
        <p>
            Author<br />
            <input type="text" id="AuthorName" name="AuthorName" value="@Model.AuthorName"/></p>
        @Html.ValidationMessageFor(model => model.AuthorName)
    </div>
   
       <div class="editor-label">
        @Html.LabelFor(model => model.Tag)
    </div>
     <div class="editor-field">
        @Html.EditorFor(model => model.Tag)
        @Html.ValidationMessageFor(model => model.Tag)
    
    </div> 
    <div class="editor-label">
        @Html.LabelFor(model => model.HasChapters)
    </div> 
      <div class="editor-field">
        @Html.CheckBoxFor(model => model.HasChapters)
    </div>
    <div class="editor-label">
    <p>Category</p>
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.CategoryId, categories)
        
    </div>
     <div class="editor-label">
        @Html.LabelFor(model => model.Description)
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.Description)
        @Html.ValidationMessageFor(model => model.Description)
    </div> 
    <div>@Html.HiddenFor(model => model.Id)</div>
    <div>@Html.HiddenFor(model => model.UploadedBy)</div>
    <div>@Html.HiddenFor(model => model.UploadedDate)</div>
        </div>
    </div>
</fieldset>
 }
<p>
     <a  type="button"  id="btnDelete" >Delete</a>  |
    @Html.ActionLink("Back to List", "Index", "Books", new { userId = @Model.UploadedBy }, "null") | @Html.ActionLink("Creat new chapter", "Create", "Chapters", new { bookid = Model.Id }, "null")
    |
</p>
   
<div id="list-book" data-bind="template: {name:viewmodel.chapterTemplate(), foreach: chapterCollection}"
</div>
@if (User.Identity.IsAuthenticated)
{ 
    <div id="create-new-chapter">
        @if (Model.NumberOfChapters == 0)
        {
            @:You haven't uploaded any chapter yet.
        }
        @Html.ActionLink("Click here", "Create", "Chapters", new { bookId = @Model.Id }, null)
        to upload a new chapter
    </div>
}
@Html.Partial("_ListChapterTemplate")

@Html.Partial("DetailsJS")
<script type="text/javascript">
 $(document).ready(function () {  
        $("#AuthorName").autocomplete("/Authors/Search", { minChars: 1 });
        $("#file_upload").uploadify({
            'uploader': '/Scripts/uploadify/uploadify.swf',
            'script': '@Url.Action("UploadImage")',
            'fileDataName': 'FileData',
            'buttonText': 'Select files',
            'multi': false,
            'width': 120,
            'sizeLimit': 4000000,
            'simUploadLimit': 1,
            'cancelImg': '/Scripts/uploadify/cancel.png',
            'folder': '/Images',
            'fileExt': '*.jpg;*.bmp;*.png;*.gif',
            'auto': true,
            'removeCompleted': false,
            'onError': function (a, b, c, d) {
                if (d.status == 404)
                    alert("Could not find upload script. Use a path relative to: " + "<?= getcwd() ?>");
                else if (d.type === "HTTP")
                    alert("error " + d.type + ": " + d.status);
                else if (d.type === "File Size")
                    alert(c.name + " " + d.type + " Limit: " + Math.round(d.info / (1024 * 1024)) + "MB");
                else
                    alert("error " + d.type + ": " + d.text);
            },
            'onComplete': function (event, queueId, fileObj, response, data) {
                $("#filesUploaded").val(fileObj.filePath);

            }

        });
    });
//    @*deletebutton*@
   $('#btnDelete').click (function(){
      var answer = confirm("Do you want to delete this book?")
        var id =@Model.Id;
   if (answer) {
            $.ajax({
                       url: '@Url.Action("DeleteConfirmed", "Books")',
                       type: "POST",
                       data: JSON.stringify({ id: @Model.Id }),
                       dataType: "json",
                       contentType:"application/json; charset=utf-8",
                       complete:function(){
                          window.location = '@Url.Action("Index", "Books")'
                       }
                   });
                }
           else {
           alert("what the fuck do you want?????")
           }
   });

</script>
